//
//  RegisterViewController.m
//  HWZB
//
//  Created by 许成雄 on 2017/4/22.
//  Copyright © 2017年 wonders. All rights reserved.
//

#import "RegisterViewController.h"
#import "NSString+Utils.h"
#import "UserAgreementViewController.h"
#import "NSString+Utils.h"
#import "RegexKit.h"
#import "MBProgressHUD+Toast.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import "HWZBAPIRequest.h"
#import "VerifyButton.h"

@interface RegisterViewController ()

@property (strong, nonatomic) UITextField *usernameTextField;
@property (strong, nonatomic) UITextField *verifyCodeTextField;
@property (strong, nonatomic) VerifyButton *verifyCodeButton;
@property (strong, nonatomic) UITextField *passwordTextField;
@property (strong, nonatomic) UITextField *retryPasswordTextField;
@property (strong, nonatomic) UIButton *protocolButton;
@property (strong, nonatomic) UIButton *registerButton;
@property (strong, nonatomic) UIButton *backButton;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = I_COLOR_WHITE;
    self.navigationItem.title = @"注册";
    [self setBackNavgationItem];
    
    [self createUI];
    [self loadData];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    if(self.verifyCodeButton) {
        [self.verifyCodeButton stop];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - UIButton Action
- (void)verifyCodeButtonAction:(id)sender {
    NSString *mobile = self.usernameTextField.text;
    if([NSString isBlankString:mobile]) {
        [MBProgressHUD showToast:@"请输入手机号码" initWithView:self.view];
        return;
    } else if(![RegexKit validateMobile:mobile]) {
        [MBProgressHUD showToast:@"请输入正确的手机号码" initWithView:self.view];
        return;
    }
    VerifyButton *button = (VerifyButton *)sender;
    [button start];
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    [params setObject:mobile forKey:@"mobile"];
    [SVProgressHUD showWithStatus:@"正在获取验证码..." maskType:SVProgressHUDMaskTypeClear];
    [[HWZBAPIRequest sharedInstance] getRegisterVerifyCodeWithParams:params success:^(NSString *verifyCode) {
        [SVProgressHUD dismiss];
        if(verifyCode != nil) {
            [MBProgressHUD showToast:@"已发送验证码, 请注意查收" initWithView:self.view];
        } else {
            [MBProgressHUD showToast:@"获取验证码失败" initWithView:self.view];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
         [MBProgressHUD showToast:[error localizedDescription] initWithView:self.view];
    }];
}

- (void)protocolButtonAction:(id)sender {
    UserAgreementViewController *agreementViewController = [[UserAgreementViewController alloc] init];
    [self.navigationController pushViewController:agreementViewController animated:YES];
}

- (void)registerButtonAction:(id)sender {
    NSString *username = self.usernameTextField.text;
    NSString *verifyCode = self.verifyCodeTextField.text;
    NSString *password = self.passwordTextField.text;
    NSString *retryPassword = self.retryPasswordTextField.text;
    if([NSString isBlankString:username]) {
        [MBProgressHUD showToast:@"请输入手机号码" initWithView:self.view];
        return;
    } else if(![RegexKit validateMobile:username]) {
        [MBProgressHUD showToast:@"请输入正确的手机号码" initWithView:self.view];
        return;
    }
    if([NSString isBlankString:verifyCode]) {
        [MBProgressHUD showToast:@"请输入验证码" initWithView:self.view];
        return;
    }
    if([NSString isBlankString:password]) {
        [MBProgressHUD showToast:@"请输入密码" initWithView:self.view];
        return;
    } else if(password.length < 6 || password.length > 18) {
        [MBProgressHUD showToast:@"密码过短，正常密码长度为6~18位" initWithView:self.view];
        return;
    }
    if([NSString isBlankString:retryPassword]) {
        [MBProgressHUD showToast:@"请再次输入密码" initWithView:self.view];
        return;
    } else if(![retryPassword isEqualToString:password]) {
        [MBProgressHUD showToast:@"两次输入的密码不一致，请重新输入" initWithView:self.view];
        return;
    }

    //用户注册
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    [params setObject:@"1" forKey:@"customerType"];
    [params setObject:username forKey:@"mobile"];
    [params setObject:password forKey:@"password"];
    [params setObject:verifyCode forKey:@"checkRand"];
    [params setObject:retryPassword forKey:@"repeatPassword"];
    [SVProgressHUD showWithStatus:@"正在用户注册..." maskType:SVProgressHUDMaskTypeClear];
    [[HWZBAPIRequest sharedInstance] userRegisterWithParams:params success:^(RegisterResultInfo *resultInfo) {
        [SVProgressHUD dismiss];
        if(resultInfo != nil) {
            [self performSelector:@selector(dismissThisViewController) withObject:nil afterDelay:1.5f];
        } else {
            [MBProgressHUD showToast:@"用户注册失败，请稍后再试" initWithView:self.view];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
        [MBProgressHUD showToast:[error localizedDescription] initWithView:self.view];
    }];
}

- (void)dismissThisViewController {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)backButtonAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Private Method
- (void)createUI {
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT- 64.0f - TRANS_VALUE(98.0f))];
    bgImageView.contentMode = UIViewContentModeScaleToFill;
    bgImageView.image = [UIImage imageNamed:@"bg_login"];
    [self.view addSubview:bgImageView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(60.0f), SCREEN_WIDTH, TRANS_VALUE(30.0f))];
    titleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(14.0f)];
    titleLabel.textColor = I_COLOR_WHITE;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"注册";
    [bgImageView addSubview:titleLabel];
    
    UIView *usernameView = [[UIView alloc] initWithFrame:CGRectMake(TRANS_VALUE(30.0f), TRANS_VALUE(90.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(30.0f), TRANS_VALUE(40.0f))];
    usernameView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.3f];
    [self.view addSubview:usernameView];
    
    UIView *imgUsernameBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, TRANS_VALUE(40.0f), TRANS_VALUE(40.0f))];
    imgUsernameBg.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.2f];
    UIImageView *usernameImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(12.5f), TRANS_VALUE(9.0f), TRANS_VALUE(15.0f), TRANS_VALUE(22.0f))];
    usernameImageView.contentMode = UIViewContentModeCenter;
    usernameImageView.image = [UIImage imageNamed:@"ic_login_mobile_phone"];
    [imgUsernameBg addSubview:usernameImageView];
    [usernameView addSubview:imgUsernameBg];
    
    self.usernameTextField = [[UITextField alloc] initWithFrame:CGRectMake(TRANS_VALUE(46.0f), TRANS_VALUE(0.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(30.0f) - TRANS_VALUE(52.0f), TRANS_VALUE(40.0f))];
    self.usernameTextField.font = [UIFont systemFontOfSize:TRANS_VALUE(14.0f)];
    self.usernameTextField.textColor = I_COLOR_WHITE;
    self.usernameTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入手机号" attributes:@{NSForegroundColorAttributeName:I_COLOR_LIGHT_WHITE}];
    self.usernameTextField.tintColor = I_COLOR_WHITE;
    [usernameView addSubview:self.usernameTextField];
    self.usernameTextField.keyboardType = UIKeyboardTypeNumberPad;
    
    UIView *verifyCodeView = [[UIView alloc] initWithFrame:CGRectMake(TRANS_VALUE(30.0f), TRANS_VALUE(134.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(30.0f), TRANS_VALUE(40.0f))];
    verifyCodeView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.3f];
    [self.view addSubview:verifyCodeView];
    
    UIView *verifyCodeBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, TRANS_VALUE(40.0f), TRANS_VALUE(40.0f))];
    verifyCodeBg.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.2f];
    UIImageView *verifyCodeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(10.5f), TRANS_VALUE(10.5f), TRANS_VALUE(19.0f), TRANS_VALUE(19.0f))];
    verifyCodeImageView.contentMode = UIViewContentModeCenter;
    verifyCodeImageView.image = [UIImage imageNamed:@"ic_login_verify_code"];
    [verifyCodeBg addSubview:verifyCodeImageView];
    [verifyCodeView addSubview:verifyCodeBg];
    
    self.verifyCodeTextField = [[UITextField alloc] initWithFrame:CGRectMake(TRANS_VALUE(46.0f), TRANS_VALUE(0.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(30.0f) - TRANS_VALUE(50.0f) - TRANS_VALUE(88.0f), TRANS_VALUE(40.0f))];
    self.verifyCodeTextField.font = [UIFont systemFontOfSize:TRANS_VALUE(14.0f)];
    self.verifyCodeTextField.textColor = I_COLOR_WHITE;
    self.verifyCodeTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入验证码" attributes:@{NSForegroundColorAttributeName:I_COLOR_LIGHT_WHITE}];
    self.verifyCodeTextField.tintColor = I_COLOR_WHITE;
    [verifyCodeView addSubview:self.verifyCodeTextField];
    self.verifyCodeTextField.keyboardType = UIKeyboardTypeNumberPad;
    
    UIView *dividerView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 2 * TRANS_VALUE(30.0f) - TRANS_VALUE(88.0f), TRANS_VALUE(5.0f), TRANS_VALUE(1.0f), TRANS_VALUE(30.0f))];
    dividerView.backgroundColor = I_COLOR_WHITE;
    [verifyCodeView addSubview:dividerView];
    
    self.verifyCodeButton = [[VerifyButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 2 * TRANS_VALUE(30.0f) - TRANS_VALUE(87.0f), TRANS_VALUE(0.0f), TRANS_VALUE(87.0f), TRANS_VALUE(40.0f)) withTitle:@"获取验证码"];
    [verifyCodeView addSubview:self.verifyCodeButton];
    [self.verifyCodeButton addTarget:self action:@selector(verifyCodeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *passwordView = [[UIView alloc] initWithFrame:CGRectMake(TRANS_VALUE(30.0f), TRANS_VALUE(178.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(30.0f), TRANS_VALUE(40.0f))];
    passwordView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.3f];
    [self.view addSubview:passwordView];
    
    UIView *imgPasswordBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, TRANS_VALUE(40.0f), TRANS_VALUE(40.0f))];
    imgPasswordBg.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.2f];
    UIImageView *passwordImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(12.5f), TRANS_VALUE(9.0f), TRANS_VALUE(15.0f), TRANS_VALUE(22.0f))];
    passwordImageView.contentMode = UIViewContentModeCenter;
    passwordImageView.image = [UIImage imageNamed:@"ic_login_password"];
    [imgPasswordBg addSubview:passwordImageView];
    [passwordView addSubview:imgPasswordBg];
    
    self.passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(TRANS_VALUE(46.0f), TRANS_VALUE(0.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(30.0f) - TRANS_VALUE(52.0f), TRANS_VALUE(40.0f))];
    self.passwordTextField.font = [UIFont systemFontOfSize:TRANS_VALUE(14.0f)];
    self.passwordTextField.textColor = I_COLOR_WHITE;
    self.passwordTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入密码" attributes:@{NSForegroundColorAttributeName:I_COLOR_LIGHT_WHITE}];
    self.passwordTextField.tintColor = I_COLOR_WHITE;
    [self.passwordTextField setSecureTextEntry:YES];
    [passwordView addSubview:self.passwordTextField];
    
    UIView *retryPasswordView = [[UIView alloc] initWithFrame:CGRectMake(TRANS_VALUE(30.0f), TRANS_VALUE(222.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(30.0f), TRANS_VALUE(40.0f))];
    retryPasswordView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.3f];
    [self.view addSubview:retryPasswordView];
    
    UIView *imgRetryPasswordBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, TRANS_VALUE(40.0f), TRANS_VALUE(40.0f))];
    imgRetryPasswordBg.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.2f];
    UIImageView *retryPasswordImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(12.5f), TRANS_VALUE(9.0f), TRANS_VALUE(15.0f), TRANS_VALUE(22.0f))];
    retryPasswordImageView.contentMode = UIViewContentModeCenter;
    retryPasswordImageView.image = [UIImage imageNamed:@"ic_login_password"];
    [imgRetryPasswordBg addSubview:retryPasswordImageView];
    [retryPasswordView addSubview:imgRetryPasswordBg];
    
    self.retryPasswordTextField = [[UITextField alloc] initWithFrame:CGRectMake(TRANS_VALUE(46.0f), TRANS_VALUE(0.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(30.0f) - TRANS_VALUE(52.0f), TRANS_VALUE(40.0f))];
    self.retryPasswordTextField.font = [UIFont systemFontOfSize:TRANS_VALUE(14.0f)];
    self.retryPasswordTextField.textColor = I_COLOR_WHITE;
    self.retryPasswordTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请再次输入密码" attributes:@{NSForegroundColorAttributeName:I_COLOR_LIGHT_WHITE}];
    self.retryPasswordTextField.tintColor = I_COLOR_WHITE;
    [self.retryPasswordTextField setSecureTextEntry:YES];
    [retryPasswordView addSubview:self.retryPasswordTextField];
    
    self.protocolButton = [[UIButton alloc] initWithFrame:CGRectMake(TRANS_VALUE(30.0f), TRANS_VALUE(262.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(30.0f), TRANS_VALUE(30.0f))];
    [self.protocolButton setBackgroundColor:[UIColor colorWithWhite:1.0f alpha:0.0f]];
    [self.protocolButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    self.protocolButton.titleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(12.0f)];
    [self.protocolButton setImage:[UIImage imageNamed:@"ic_login_forget_password"] forState:UIControlStateNormal];
    [self.protocolButton setImage:[UIImage imageNamed:@"ic_login_forget_password"] forState:UIControlStateSelected];
    [self.protocolButton setImage:[UIImage imageNamed:@"ic_login_forget_password"] forState:UIControlStateHighlighted];
    [self.protocolButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, TRANS_VALUE(4.0f))];
    [self.protocolButton setTitleColor:I_COLOR_WHITE forState:UIControlStateNormal];
    [self.protocolButton setTitleColor:I_COLOR_WHITE forState:UIControlStateSelected];
    [self.protocolButton setTitle:@"注册代表你已同意铧为至宝用户协议" forState:UIControlStateNormal];
    [self.protocolButton setTitle:@"忘记密码" forState:UIControlStateSelected];
    [self.view addSubview:self.protocolButton];
    [self.protocolButton addTarget:self action:@selector(protocolButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.registerButton = [[UIButton alloc] initWithFrame:CGRectMake(TRANS_VALUE(30.0f), TRANS_VALUE(308.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(30.0f), TRANS_VALUE(40.0f))];
    [self.registerButton setBackgroundColor:[UIColor colorWithWhite:1.0f alpha:0.8]];
    self.registerButton.titleLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(14.0f)];
    [self.registerButton setTitleColor:I_COLOR_RED forState:UIControlStateNormal];
    [self.registerButton setTitleColor:I_COLOR_RED forState:UIControlStateNormal];
    [self.registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [self.registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [self.view addSubview:self.registerButton];
    [self.registerButton addTarget:self action:@selector(registerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.backButton = [[UIButton alloc] initWithFrame:CGRectMake(TRANS_VALUE(30.0f), TRANS_VALUE(352.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(30.0f), TRANS_VALUE(40.0f))];
    [self.backButton setBackgroundColor:[UIColor colorWithWhite:1.0f alpha:0.4]];
    self.backButton.titleLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(14.0f)];
    [self.backButton setTitleColor:I_COLOR_WHITE forState:UIControlStateNormal];
    [self.backButton setTitleColor:I_COLOR_WHITE forState:UIControlStateSelected];
    [self.backButton setTitle:@"返回登录" forState:UIControlStateNormal];
    [self.backButton setTitle:@"返回登录" forState:UIControlStateSelected];
    [self.view addSubview:self.backButton];
    [self.backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *logoImageView = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - TRANS_VALUE(208)) / 2, SCREEN_HEIGHT- 64.0f - TRANS_VALUE(78.0f), TRANS_VALUE(208.0f), TRANS_VALUE(58.0f))];
    logoImageView.contentMode = UIViewContentModeScaleAspectFit;
    logoImageView.image = [UIImage imageNamed:@"ic_login_logo"];
    [self.view addSubview:logoImageView];
    
}

- (void)loadData {
    
}

@end
